package area

import (
	"Project/global"
	"Project/model/area/request"
	areaRep "Project/model/area/response"
	"Project/model/common/response"
	"Project/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log"
)


type AreaUserApi struct {}

// EditorAreaUserInfo 修改个人信息
func (a *AreaUserApi) EditorAreaUserInfo(c *gin.Context)  {
	var userInfo request.AreaUserInfoRes
	if err := c.ShouldBindJSON(&userInfo);err != nil{
		log.Println("获取参数失败",err)
		global.GVA_LOG.Error("获取参数失败",zap.Error(err))
		response.FailWithMessage("参数绑定失败",c)
		return
	}
	userID := utils.GetUserID(c)
	if err := areaUserService.EditorAreaUserInfo(userID,userInfo);err != nil {
		log.Println("修改信息失败",err)
		global.GVA_LOG.Error("修改信息失败",zap.Error(err))
		response.FailWithMessage("修改信息失败",c)
		return
	}
	response.Ok(c)

}

// EditorPassword 修改密码
func (a *AreaUserApi) EditorPassword(c *gin.Context)  {
	var passwordInfo request.PasswordInfo
	if err := c.ShouldBindJSON(&passwordInfo);err != nil{
		log.Println("获取参数失败",err)
		global.GVA_LOG.Error("获取参数失败",zap.Error(err))
		response.FailWithMessage("参数绑定失败",c)
		return
	}
	userID := utils.GetUserID(c)
	if err := areaUserService.EditorPassword(userID,passwordInfo);err != nil {
		log.Println("修改信息失败",err)
		global.GVA_LOG.Error("修改信息失败",zap.Error(err))
		response.FailWithMessage("修改信息失败",c)
		return
	}
	response.Ok(c)

}

// UserInfo 获取用户信息
func (a *AreaUserApi) UserInfo(c *gin.Context)  {
	userID := utils.GetUserID(c)
	user,err := areaUserService.GetUserInfoByID(userID)
	if err != nil {
		log.Println("修改信息失败",err)
		global.GVA_LOG.Error("修改信息失败",zap.Error(err))
		response.FailWithMessage("修改信息失败",c)
		return
	}
	data := areaRep.AreaUserInfoRep{
		UserName: user.UserName,
		Card: user.Card,
		Phone: user.Phone,
	}
	response.OkWithDetailed(data,"查询成功",c)

}